import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket=null;
    private String severIp="";
    private int serverPort=0;

    public UdpEchoClient(String severIp,int port) throws SocketException {
        socket=new DatagramSocket();
        severIp=severIp;
        serverPort=port;
    }

    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.print("->");
            String request=scanner.next();

            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(severIp),serverPort);
            socket.send(requestPacket);

            DatagramPacket responsePacket=new DatagramPacket(new byte[4090],4090);
            socket.receive(requestPacket);

            String response=new String(requestPacket.getData(),0,requestPacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String [] args) throws IOException {
        UdpEchoClient client=new UdpEchoClient("192.168.3.1",9090);
        client.start();
    }
}
